using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using logistical.Models;

namespace logistical.Controllers
{
    public class sys_roleController : baseController
    {
        public class list_sys_role
        {
            public List<sys_role> objs { get; set; }
        }

        [HttpGet]
        public object get()
        {
            try
            {
                var o = new logisticalEntities();
                var p = o.sys_role;

                return new { 
                                data = p

                            };
            }
            catch (Exception ex)
            {
                return new { msg = ex.Message };
            }
        }


        [HttpPost]
        public object add(sys_role obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                obj.id = Guid.NewGuid().ToString();
                common.sysEval.evalCurUserAndDate(obj);
                o.sys_role.Add(obj);
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object edit(sys_role obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.sys_role.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.sys_role.SingleOrDefault(x => x.id == obj.id);
old.code=obj.code;
old.name=obj.name;
                    common.sysEval.evalCurUserAndDate(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object del(sys_role obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.sys_role.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.sys_role.SingleOrDefault(x => x.id == obj.id);
                    o.sys_role.Remove(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object mdel(list_sys_role objs)
        {
            string msg = "";
            try
            {
            var o = new logisticalEntities();
            foreach (var p in objs.objs)
            {
                var co = o.sys_role.Where(x => x.id == p.id);
                if (co.Count() == 1)
                {
                    var old = o.sys_role.SingleOrDefault(x => x.id == p.id);
                    o.sys_role.Remove(old);
                }
            }
            o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }
    }
}
